class FormatString {
    constructor() {
        //四舍五入保留N位小数 自动补全
        this.formatFloat = (x, pos) => {
            let f = parseFloat(x)
            if (isNaN(f)) {
                return 0
            }
            let n = Math.pow(10, pos)
            let k = Math.round(x * n) / n
            let s = k.toString()
            let rs = s.indexOf('.')
            if (rs < 0 && pos > 0) {
                rs = s.length
                s += '.'
            }
            while (s.length <= rs + pos) {
                s += '0'
            }
            return s
        }
        // 货币格式化
        this.C = (data, pos) => {
            return `￥${this.N(data, pos)}`
        }
        // 逗号隔开 保留n位小数
        this.N = (data, pos) => {
            if (pos <= 0) {
                let floatNum = String(data).split('.')[1]
                if (floatNum) {
                    pos = floatNum.length
                } else {
                    pos = 0
                }
            }
            let strData = this.formatFloat(data, pos)
            let minusSign = ''
            if (strData < 0) {
                minusSign = '-'
            }
            let intNum = String(Math.abs(strData)).split('.')[0]
            if (intNum.length <= 3) {
                return strData
            } else {
                let reverseNum = intNum.split('').reverse()
                let reverseNullNum = []
                reverseNum.map((item, index) => {
                    reverseNullNum.unshift(item)
                    if ((index + 1) % 3 === 0) {
                        if (index + 1 !== reverseNum.length) {
                            reverseNullNum.unshift(',')
                        }
                    }
                })
                if (pos === 0) {
                    return minusSign + reverseNullNum.join('')
                } else {
                    return minusSign + reverseNullNum.join('') + '.' + strData.split('.')[1]
                }
            }
        }
        // 保留n位小数
        this.F = (data, pos) => {
            return this.formatFloat(data, pos)
        }
        // 百分比格式化
        this.P = (data, pos) => {
            return this.formatFloat(data * 100, pos) + '%'
        }
    }
    /**
     * date 日期
     * fmt
     *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
     *年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
     */
    formatDate(fmt, date) {
        if (!date) {
            return ''
        }
        let o = {
            'M+': new Date(date).getMonth() + 1, //月份
            'd+': new Date(date).getDate(), //日
            'h+': new Date(date).getHours(), //小时
            'm+': new Date(date).getMinutes(), //分
            's+': new Date(date).getSeconds(), //秒
            'q+': Math.floor((new Date(date).getMonth() + 3) / 3), //季度
            S: new Date(date).getMilliseconds() //毫秒
        }
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (new Date(date).getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        for (let k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
            }
        }
        if (fmt.includes('NaN')) {
            return ''
        } else {
            return fmt
        }
    }
    // 数字格式化
    formatNumber(format, data) {
        let pos = format.substr(1, format.length)
        if (pos) {
            pos = Number(pos)
        } else {
            pos = 2
        }
        let FormatFn = format.substr(0, 1)
        let returnData = this[FormatFn](data, pos)
        if (returnData.includes && returnData.includes('NaN')) {
            return '0'
        } else {
            return returnData
        }
    }
    // 金额转大写
    convertCurrency(formatStr, money) {
        //汉字的数字
        const cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖')
        //基本单位
        const cnIntRadice = new Array('', '拾', '佰', '仟')
        //对应整数部分扩展单位
        const cnIntUnits = new Array('', '万', '亿', '兆')
        //对应小数部分单位
        const cnDecUnits = new Array('角', '分', '毫', '厘')
        //整数金额时后面跟的字符
        const cnInteger = '整'
        //整型完以后的单位
        const cnIntLast = '元'
        //最大处理的数字
        const maxNum = 999999999999999.9999
        //金额整数部分
        let integerNum
        //金额小数部分
        let decimalNum
        //输出的中文金额字符串
        let chineseStr = ''
        //分离金额后用的数组，预定义
        let parts
        if (money === '') {
            return ''
        }
        money = parseFloat(money)
        if (money >= maxNum) {
            //超出最大处理数字
            return ''
        }
        if (money === 0) {
            chineseStr = cnNums[0] + cnIntLast + cnInteger
            return chineseStr
        }
        //转换为字符串
        money = money.toString()
        if (money.indexOf('.') === -1) {
            integerNum = money
            decimalNum = ''
        } else {
            parts = money.split('.')
            integerNum = parts[0]
            decimalNum = parts[1].substr(0, 4)
        }
        //获取整型部分转换
        if (parseInt(integerNum, 10) > 0) {
            let zeroCount = 0
            let IntLen = integerNum.length
            for (let i = 0; i < IntLen; i++) {
                let n = integerNum.substr(i, 1)
                let p = IntLen - i - 1
                let q = p / 4
                let m = p % 4
                if (n === '0') {
                    zeroCount++
                } else {
                    if (zeroCount > 0) {
                        chineseStr += cnNums[0]
                    }
                    //归零
                    zeroCount = 0
                    chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
                }
                if (m === 0 && zeroCount < 4) {
                    chineseStr += cnIntUnits[q]
                }
            }
            chineseStr += cnIntLast
        }
        //小数部分
        if (decimalNum !== '') {
            var decLen = decimalNum.length
            for (var i = 0; i < decLen; i++) {
                var n = decimalNum.substr(i, 1)
                if (n !== '0') {
                    chineseStr += cnNums[Number(n)] + cnDecUnits[i]
                }
            }
        }
        if (chineseStr === '') {
            chineseStr += cnNums[0] + cnIntLast + cnInteger
        } else if (decimalNum === '') {
            chineseStr += cnInteger
        }
        return chineseStr
    }
}
export const format = (dataType, formatStr, formatData) => {
    let formatFn = new FormatString()
    if (dataType === 5 || dataType === '5') {
        return formatFn.formatDate(formatStr, formatData)
    } else if (dataType === 3 || dataType === '3') {
        return formatFn.formatNumber(formatStr, formatData)
    } else if (dataType === 2 || dataType === '2') {
        return formatFn.convertCurrency(formatStr, formatData)
    }
}
